package com.example.thirdhomework

import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class FourthActivity : AppCompatActivity() {
    private lateinit var timerTextView: TextView
    private lateinit var startButton: Button
    private lateinit var stopButton: Button
    private lateinit var resetButton: Button

    private var handler: Handler = Handler()
    private var startTime: Long = 0L
    private var timeInMilliseconds: Long = 0L
    private var timeSwapBuff: Long = 0L
    private var updateTime: Long = 0L

    private val updateTimerThread: Runnable = object : Runnable {
        override fun run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime
            updateTime = timeSwapBuff + timeInMilliseconds
            val secs = (updateTime / 1000).toInt()
            val mins = secs / 60
            val milliseconds = (updateTime % 1000).toInt()
            timerTextView.text = String.format("%02d:%02d:%03d", mins, secs % 60, milliseconds)
            handler.postDelayed(this, 0)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)

        timerTextView = findViewById(R.id.timerTextView)
        startButton = findViewById(R.id.startButton)
        stopButton = findViewById(R.id.stopButton)
        resetButton = findViewById(R.id.resetButton)

        startButton.setOnClickListener {
            startTime = SystemClock.uptimeMillis()
            handler.postDelayed(updateTimerThread, 0)
            startButton.isEnabled = false
            stopButton.isEnabled = true
        }

        stopButton.setOnClickListener {
            timeSwapBuff += timeInMilliseconds
            handler.removeCallbacks(updateTimerThread)
            startButton.isEnabled = true
            stopButton.isEnabled = false
        }

        resetButton.setOnClickListener {
            handler.removeCallbacks(updateTimerThread) // 停止计时器线程
            startTime = 0L
            timeInMilliseconds = 0L
            timeSwapBuff = 0L
            updateTime = 0L
            timerTextView.text = "00:00:000"
            startButton.isEnabled = true
            stopButton.isEnabled = false
        }

        if (savedInstanceState != null) {
            startTime = savedInstanceState.getLong("startTime")
            timeInMilliseconds = savedInstanceState.getLong("timeInMilliseconds")
            timeSwapBuff = savedInstanceState.getLong("timeSwapBuff")
            updateTime = savedInstanceState.getLong("updateTime")
            timerTextView.text = savedInstanceState.getString("timerText")
            if (savedInstanceState.getBoolean("isRunning")) {
                handler.postDelayed(updateTimerThread, 0)
                startButton.isEnabled = false
                stopButton.isEnabled = true
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putLong("startTime", startTime)
        outState.putLong("timeInMilliseconds", timeInMilliseconds)
        outState.putLong("timeSwapBuff", timeSwapBuff)
        outState.putLong("updateTime", updateTime)
        outState.putString("timerText", timerTextView.text.toString())
        outState.putBoolean("isRunning", !startButton.isEnabled)
    }
}